package com.wsd.jing.controller.system;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.wsd.jing.dto.LoginDTO;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.ILoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: Login
 * @description 登录前端控制器
 * @createDate 2022/11/14 11:12
 * @copyright 2022/11/14 11:12 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "登录")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/auth")
public class LoginController {

    private final ILoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public CommonResponse login(@Validated @RequestBody LoginDTO query, HttpServletRequest httpServletRequest) {
        SaTokenInfo saTokenInfo = loginService.login(query, httpServletRequest);
        return CommonResponse.success(saTokenInfo, "登录成功");
    }

    @ApiOperation(value = "查询验证码")
    @PostMapping("captcha")
    public CommonResponse getCaptcha() {
        Map resultMap = loginService.getCaptcha();
        return CommonResponse.success(resultMap, "查询成功");
    }

    @ApiOperation(value = "注销")
    @PostMapping("logout")
    public CommonResponse logout() {
        StpUtil.logout();
        return CommonResponse.success("注销成功");
    }
}
